package com.cjm.one.common.filter;

import com.cjm.one.common.utils.RequestPathContextHolder;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class PathDetectionFilter implements Filter {
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        // 获取请求路径并存储
        RequestPathContextHolder.setPath(httpRequest.getRequestURI());
        try {
            chain.doFilter(request, response);
        } finally {
            RequestPathContextHolder.clear(); // 必须清理线程变量
        }
    }
}
